package homewoek12月30号.homework.h3;

import cn.itcast.xml.XmlUtils;

import java.util.*;

public class Demo1 {
    public static void main(String[] args) {
        getStudents();
        getCourses();
        getScores();
        System.out.println(getCourses());
        System.out.println(getStudents());
        System.out.println(getScores());
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名：");
        String sname = sc.next();
        System.out.println("课程名称：");
        String cname = sc.next();
        List<HashMap<String, String>> haslist = getStuScore(sname, cname);

        ArrayList<Map.Entry> lsit = new ArrayList<>();
        for (HashMap<String, String> map : haslist) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                lsit.add(entry);
            }
        }
        System.out.println("排序前：");
        for (Map.Entry entry : lsit) {
            System.out.println("sname="+sname+",cname="+entry.getKey()+"="+entry.getValue());
        }
        Collections.sort(lsit, new Comparator<Map.Entry>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o3) {
                int x = Integer.parseInt((String) o1.getValue()) - Integer.parseInt((String) o3.getValue());
                return x;
            }
        });
        System.out.println("排序后：");
        for (Map.Entry entry : lsit) {
            System.out.println("sname="+sname+",cname="+entry.getKey()+"="+entry.getValue());
        }
    }
    public static List<HashMap<String,String>> getStuScore(String sname, String cname){
        List<Stuent> slist = getStudents();
        List<Course> clist = getCourses();
        List<Score> sclist = getScores();
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        for (Stuent stuent : slist) {
            if (stuent.getSname().equals(sname)){
                for (Course course : clist) {
                    if (cname.equals(course.getCname())){
                        for (Score score : sclist) {
                            if (course.getCid().equals(score.getCid())){
                                map.put(cname+".score",score.getScore());
                                break;
                            }
                        }
                    }if (cname.equals("null")){
                        for (Score score : sclist) {
                            if (stuent.getSid().equals(score.getSid())){
                                for (Course cou : clist) {
                                    if (score.getCid().equals(cou.getCid())){
                                        map.put(cou.getCname()+".score",score.getScore());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        list.add(map);
        return list;
    }
    public static List< Score> getScores (){
        Score score = new Score();
        List<? extends Score> slist = XmlUtils.readAll("src/czxy/com/homework/h3/score.xml", score.getClass());
        return (List<Score>) slist;
    }
    public static List< Course > getCourses (){
        Course course = new Course();
        List<? extends Course> list = XmlUtils.readAll("src/czxy/com/homework/h3/course.xml", course.getClass());
        ArrayList<Course> clist = new ArrayList<>();
        for (Course c : list) {
            if (!clist.contains(c)){
                clist.add(c);
            }
        }
        return clist;
    }
    public static List<Stuent> getStudents(){
        Stuent stuent = new Stuent();
        List<? extends Stuent> slist = XmlUtils.readAll("src/czxy/com/homework/h3/student.xml", stuent.getClass());
        return (List<Stuent>) slist;
    }
}
